package PrjHttpUtils

import (
	"dgo/goutils/dnet"
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
	"io"
	"net/http"
)

var (
	montiroTitleConf *wrapper.SuperValue
)

func CheckDefaultConfigMonitor() {
	if montiroTitleConf == nil {
		montiroTitleConf = wrapper.NewSVObject()
		montiroTitleConf.SetKeyValues("online", "在线数",
			"max_online_cnt", "最大在线数",
			"dis_cnt", "断线次数", "con_cnt", "连接次数",
			"recv_bytes", "接收字节", "send_bytes", "发送字节",
			"speed_recvbytes", "接收字节(每秒)", "speed_sendbytes", "发送字节(每秒)",
			"recv_io", "接收IO(次)", "send_io", "发送IO(次)",
			"speed_recvio", "接收IO(次/s)", "speed_sendio", "发送IO(次/s)",
			"recvio", "接收IO", "sendio", "发送IO",
			"recvinfo", "接收信息", "sendinfo", "发送信息",
			"run-secs", "持续运行时间")
	}
}

func ConfigMonitorTitle(kv ...interface{}) {
	CheckDefaultConfigMonitor()
	montiroTitleConf.SetKeyValues(kv...)
}

func ResponseMonitorFunc(w http.ResponseWriter, exinfofn func(json *wrapper.SuperValue)) {
	utils.Monitor.CheckStartSpeedTask()
	recMonitor := wrapper.NewSVObject()
	utils.Monitor.GetInfo(func(sender interface{}, senderId string, k string, val interface{}) {
		if len(senderId) == 0 {
			recMonitor.SetKeyValue(k, val)
		} else {
			recMonitor.SetKeyValue(fmt.Sprintf("%s-%s", senderId, k), val)
		}
	})

	if exinfofn != nil {
		exinfofn(recMonitor)
	}

	recMonitor.SetKeyValue("run-secs", utils.HumanTimeStr(GetRunSecs()))
	if montiroTitleConf == nil {
		JSON2TableRec(w, "{}", recMonitor.AsJSONString(false))
	} else {
		JSON2TableRec(w, montiroTitleConf.AsJSONString(false), recMonitor.AsJSONString(false))
	}
}

func ResponseNetInfo(w io.Writer, netinfo *dnet.DNetInfo) {
	json := wrapper.NewSVObject()
	netinfo.GetInfoCb(func(k string, v interface{}) {
		if k == "recv_bytes" || k == "send_bytes" || k == "speed_sendbytes" || k == "speed_recvbytes" {
			json.SetKeyValue(k, utils.HumanFilesizeU(v.(uint64)))
		} else {
			json.SetKeyValue(k, v)
		}
	})
	json.SetKeyValue("run-secs", utils.HumanTimeStr(GetRunSecs()))
	JSON2TableRec(w, utils.BuildJSONString("online", "在线数",
		"max_online_cnt", "最大在线数",
		"ntrip_cnt", "Ntrip在线数",
		"dis_cnt", "断线次数", "con_cnt", "连接次数",
		"recvinfo", "接收信息", "sendinfo", "发送信息",
		"recvio", "接收IO", "sendio", "发送IO",
		"recv_bytes", "接收字节", "send_bytes", "发送字节",
		"speed_recvbytes", "接收字节(每秒)", "speed_sendbytes", "发送字节(每秒)",
		"recv_io", "接收IO(次)", "send_io", "发送IO(次)",
		"speed_recvio", "接收IO(次/s)", "speed_sendio", "发送IO(次/s)",
		"run-secs", "持续运行时间",
	), json.String())
}

func ResponseNetInfoEx(w io.Writer, netinfo *dnet.DNetInfo, exinfofn func(title, json *wrapper.SuperValue)) {
	json := wrapper.NewSVObject()
	title := wrapper.NewSVObject()
	netinfo.GetInfoCb(func(k string, v interface{}) {
		if k == "recv_bytes" || k == "send_bytes" || k == "speed_sendbytes" || k == "speed_recvbytes" {
			json.SetKeyValue(k, utils.HumanFilesizeU(v.(uint64)))
		} else {
			json.SetKeyValue(k, v)
		}
	})
	json.SetKeyValue("run-secs", utils.HumanTimeStr(GetRunSecs()))
	title.SetKeyValues("online", "在线数",
		"max_online_cnt", "最大在线数",
		"ntrip_cnt", "Ntrip在线数",
		"dis_cnt", "断线次数", "con_cnt", "连接次数",
		"recvinfo", "接收信息", "sendinfo", "发送信息",
		"recvio", "接收IO", "sendio", "发送IO",
		"recv_bytes", "接收字节", "send_bytes", "发送字节",
		"speed_recvbytes", "接收字节(每秒)", "speed_sendbytes", "发送字节(每秒)",
		"recv_io", "接收IO(次)", "send_io", "发送IO(次)",
		"speed_recvio", "接收IO(次/s)", "speed_sendio", "发送IO(次/s)",
		"run-secs", "持续运行时间")
	if exinfofn != nil {
		exinfofn(title, json)
	}
	JSON2TableRec(w, title.AsJSONString(false), json.String())
}
